package cn.jsl.controller;

import cn.jsl.downgrade.DowngradeUtil;
import cn.jsl.downgrade.FuseDowngradeUtil;
import cn.jsl.entity.User;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RefreshScope //动态刷新配置
@RequestMapping("/user")
public class UserController {

//    @Value("${dynamicRefreshTest}")
//    private String dynamicRefreshTest;

//    /**
//     * sentinel  限流注解
//     * value 就是一个名字
//     * blockHandler 指定阻塞后执行的方法的方法名
//     */
//    @GetMapping("/getUserById/{id}")
//    @SentinelResource(value = "user-server",blockHandler = "getUserByIdBlockHandler")
//    public User getUserById(@PathVariable("id") Long id){
//        return new User(id,"灏猪","暴打灏猪");
//    }
//
//    public User getUserByIdBlockHandler(@PathVariable("id") Long id,BlockException ex){
//        ex.printStackTrace();
//        return new User(-1L,"赟一","赟一被灏猪拱了");
//    }

    /**
     * 熔断
     */
    @GetMapping("/getUserById/{id}")
    @SentinelResource(value = "user-service",fallback = "fuseDegradation",fallbackClass = FuseDowngradeUtil.class)
    public User getUserById(@PathVariable("id") Long id){
        try {
            // 用于让服务慢响应
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return new User(id,"季节","暴打灏猪");
    }

//    public User fuseDegradation(@PathVariable("id") Long id,BlockException ex){
//        ex.printStackTrace();
//        return new User(-1L,"熔断服务降级触发！", "服务不可用，请稍后重试！");
//    }

}
